import { customRef } from "vue";

export function debounceRef(value: any, delay: number = 1000) {
  let timer: number | undefined;
  return customRef((track, trigger) => {
    return {
      get() {
        // 依赖收集
        track();
        return value;
      },
      set(newValue) {
        clearTimeout(timer);
        timer = setTimeout(() => {
          value = newValue;
          // 派发更新
          trigger();
        }, delay);
      },
    };
  });
}
